package com.irdstudio.efp.console.service.impl;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.efp.console.service.dao.SUserOrgDao;
import com.irdstudio.efp.console.service.domain.SUserOrg;
import com.irdstudio.efp.console.service.facade.SUserOrgService;
import com.irdstudio.efp.console.service.vo.SUserOrgVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
/**
 * <p>ServiceImpl: 系统机构用户				<p>
 * @author chenqm1
 * @date 2018-05-03
 */
@Service("sUserOrgService")
public class SUserOrgServiceImpl implements SUserOrgService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(SUserOrgServiceImpl.class);

	@Autowired
	private SUserOrgDao sUserOrgDao;
	
	/**
	 * 新增操作
	 */
	public int insertSUserOrg(SUserOrgVO inSUserOrgVo) {
		logger.debug("当前新增数据为:"+ inSUserOrgVo.toString());
		int num;
		try {
			SUserOrg sUserOrg = new SUserOrg();
			beanCopy(inSUserOrgVo, sUserOrg);
			num = sUserOrgDao.insertSUserOrg(sUserOrg);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}
	/**
	 * 新增修改操作
	 */
	public int insertSUserOrgOrUpdateByPk(SUserOrgVO inSUserOrgVo) {
		logger.debug("当前新增数据为:"+ inSUserOrgVo.toString());
		int num;
		try {
			SUserOrg sUserOrg = new SUserOrg();
			beanCopy(inSUserOrgVo, sUserOrg);
			num = sUserOrgDao.insertSUserOrgOrUpdateByPk(sUserOrg);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}
	/**
	 * 删除操作
	 */
	public int deleteByPk(SUserOrgVO inSUserOrgVo) {
		logger.debug("当前删除数据条件为:"+ inSUserOrgVo);
		int num;
		try {
			SUserOrg sUserOrg = new SUserOrg();
			beanCopy(inSUserOrgVo, sUserOrg);
			num = sUserOrgDao.deleteByPk(sUserOrg);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inSUserOrgVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(SUserOrgVO inSUserOrgVo) {
		logger.debug("当前修改数据为:"+ inSUserOrgVo.toString());
		int num;
		try {
			SUserOrg sUserOrg = new SUserOrg();
			beanCopy(inSUserOrgVo, sUserOrg);
			num = sUserOrgDao.updateByPk(sUserOrg);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inSUserOrgVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public SUserOrgVO queryByPk(SUserOrgVO inSUserOrgVo) {
		
		logger.debug("当前查询参数信息为:"+ inSUserOrgVo);
		try {
			SUserOrg querySUserOrg = new SUserOrg();
			beanCopy(inSUserOrgVo, querySUserOrg);
			SUserOrg queryRslSUserOrg = sUserOrgDao.queryByPk(querySUserOrg);
			if (Objects.nonNull(queryRslSUserOrg)) {
				SUserOrgVO outSUserOrgVo = beanCopy(queryRslSUserOrg, new SUserOrgVO());
				logger.debug("当前查询结果为:"+ outSUserOrgVo.toString());
				return outSUserOrgVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public List<SUserOrgVO> queryByLegalOrgCode(SUserOrgVO sUserOrgVo) {
		logger.debug("当前查询的参数信息为:");
		SUserOrg sUserOrg = beanCopy(sUserOrgVo, new SUserOrg());
		List<SUserOrgVO> list = null;
		try {
			List<SUserOrg> sUserOrgs = sUserOrgDao.queryByLegalOrgCode(sUserOrg);
			logger.debug("当前查询的结果集数量为:"+ sUserOrgs.size());
			pageSet(sUserOrgs, sUserOrg);
			list = (List<SUserOrgVO>) beansCopy(sUserOrgs, SUserOrgVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SUserOrgVO> queryAllOwner(SUserOrgVO sUserOrgVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		SUserOrg sUserOrg = beanCopy(sUserOrgVo, new SUserOrg());
		List<SUserOrgVO> list = null;
		try {
			List<SUserOrg> sUserOrgs = sUserOrgDao.queryAllOwnerByPage(sUserOrg);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ sUserOrgs.size());
			pageSet(sUserOrgs, sUserOrg);
			list = (List<SUserOrgVO>) beansCopy(sUserOrgs, SUserOrgVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SUserOrgVO> queryAllCurrOrg(SUserOrgVO sUserOrgVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		SUserOrg sUserOrg = beanCopy(sUserOrgVo, new SUserOrg());
		List<SUserOrg> sUserOrgs = sUserOrgDao.queryAllCurrOrgByPage(sUserOrg);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+sUserOrgs.size());
		List<SUserOrgVO> list = null;
		try {
			pageSet(sUserOrgs, sUserOrg);
			list = (List<SUserOrgVO>) beansCopy(sUserOrgs, SUserOrgVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SUserOrgVO> queryAllCurrDownOrg(SUserOrgVO sUserOrgVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		SUserOrg sUserOrg = beanCopy(sUserOrgVo, new SUserOrg());
		List<SUserOrg> sUserOrgs = sUserOrgDao.queryAllCurrDownOrgByPage(sUserOrg);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ sUserOrgs.size());
		List<SUserOrgVO> list = null;
		try {
			pageSet(sUserOrgs, sUserOrg);
			list = (List<SUserOrgVO>) beansCopy(sUserOrgs, SUserOrgVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}

	@Override
	public SUserOrgVO queryByOrgCode(SUserOrgVO sUserOrgVo) {
		logger.debug("当前查询参数信息为:"+ sUserOrgVo);
		try {
			SUserOrg querySUserOrg = new SUserOrg();
			beanCopy(sUserOrgVo, querySUserOrg);
			SUserOrg queryRslSUserOrg = sUserOrgDao.queryByOrgCode(querySUserOrg);
			if (Objects.nonNull(queryRslSUserOrg)) {
				SUserOrgVO outSUserOrgVo = beanCopy(queryRslSUserOrg, new SUserOrgVO());
				logger.debug("当前查询结果为:"+ outSUserOrgVo.toString());
				return outSUserOrgVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

}
